<?php
// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))
	exit;

define('PUN_ROOT', '../');

@include PUN_ROOT.'config.php';
require $gallery_full_path.'embed.php';

class g2helper
{
	/**
	* TODO:make phpbb password compatible with standard phpass used by Gallery2.3 or higher
	*
	* @param string $password current punBB user password
	*
	* @return array Gallery user password and password type
	*
	* @access private
	*/
	function set_g2_hash($password)
	{
		$g2_password = md5($password);
		$g2_passtype = 'md5';

		return array($g2_password, $g2_passtype);
	}

	function convert_pun_language($lang)
	{
		if ($lang == 'Simplified_Chinese')
			$lang_conv = 'zh_CN';
		elseif ($lang == 'Traditional_Chinese')
			$lang_conv = 'zh_TW';
		else
			$lang_conv = 'en';
		return $lang_conv;
	}

	/**
	  * This function specify the operator by $uid
	  */
	function init($uid, $language = false)
	{
		global $gallery_url, $gallery_embed_url;
		$g2_initdata = array('g2Uri' => $gallery_url, 
					'embedUri' => $gallery_embed_url,
					'activeUserId' => $uid);
		if ($language)
			$g2_initdata['activeLanguage'] = $this->convert_pun_language($language);

		$ret = GalleryEmbed::init($g2_initdata);
		if ($ret && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
		{
			list ($ret, $userId) = GalleryCoreApi::fetchUserByUserName($uid);
			if (empty($ret))
			{
				$ret = GalleryEmbed::addExternalIdMapEntry($uid, $userId->getId(), 'GalleryUser');
				if ($ret)
				{
					return $ret;
				}
			}
			else
			{
				return $ret;
			}
		}
		elseif ($ret)
		{
			return $ret;
		}
	}

	function done()
	{
		GalleryEmbed::done();
	}

	function logout()
	{
		global $gallery_embed_url;
		$ret = GalleryEmbed::logout(array($gallery_embed_url));
		if ($ret)
			return $ret;
	}

	function create_user($uid, $userdata)
	{
		if (isset($userdata['password']))
		{
			list ($g2_password_hash, $g2_passtype) = $this->set_g2_hash($userdata['password']);
		}
		else
		{
			$g2_password_hash = null;
			$g2_passtype = null;
		}
		if (isset($userdata['language']))
		{
			$g2_language = $this->convert_pun_language($userdata['language']);
		}
		else
		{
			$g2_language = null;
		}
		$ret = GalleryEmbed::createUser($uid,
			array(
				'username'			=> $userdata['username'],
				'hashedpassword'	=> $g2_password_hash,
				'hashmethod'		=> $g2_passtype,
				'email'				=> $userdata['user_email'],
				'fullname'			=> $userdata['username'],
				'language'			=> $g2_language,
				'creationtimestamp'	=> $userdata['creationtimestamp'])
			);
		if ($ret)
		{
			return $ret;
		}
	}

	function update_user($uid, $userdata)
	{
		$ret = GalleryEmbed::isExternalIdMapped($uid, 'GalleryUser');
		if (empty($ret))
		{
			$g2_userdata = array();
			if (isset($userdata['username']))
			{
				$g2_userdata['username'] = $userdata['username'];
				$g2_userdata['fullname'] = $userdata['username'];
			}
			if (isset($userdata['password']))
			{
				list ($g2_password_hash, $g2_passtype) = $this->set_g2_hash($userdata['password']);
				$g2_userdata['hashedpassword'] = $g2_password_hash;
				$g2_userdata['hashmethod'] = $g2_passtype;
			}
			if (isset($userdata['language']))
				$g2_userdata['language'] = $this->convert_pun_language($userdata['language']);
			if (isset($userdata['user_email']))
				$g2_userdata['email'] = $userdata['user_email'];

			$ret = GalleryEmbed::updateUser($uid, $g2_userdata);
			if ($ret)
			{
				return $ret;
			}
		}
		elseif ($ret && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
		{
			/*
			 * user doesn't exist in gallery, so do nothing!
			 */
		}
		else
		{
			return $ret;
		}
	}

	function delete_user($uid)
	{
		$ret = GalleryEmbed::isExternalIdMapped($uid, 'GalleryUser');
		if (empty($ret))
		{
			$ret = GalleryEmbed::deleteUser($uid);
			if ($ret)
			{
				return $ret;
			}
		}
		elseif ($ret && $ret->getErrorCode() & ERROR_MISSING_OBJECT)
		{
			/*
			 * user doesn't exist in gallery, so do nothing!
			 */
		}
		else
		{
			return $ret;
		}
	}
}

?>
